package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.system.domain.ZhwyShops;
import com.ruoyi.system.mapper.ZhwyShopsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ZhwyMeterreadingdataMapper;
import com.ruoyi.domain.ZhwyMeterreadingdata;
import com.ruoyi.service.IZhwyMeterreadingdataService;

/**
 * 抄表数据管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class ZhwyMeterreadingdataServiceImpl implements IZhwyMeterreadingdataService 
{
    @Autowired
    private ZhwyMeterreadingdataMapper zhwyMeterreadingdataMapper;
    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;

    /**
     * 查询抄表数据管理
     * 
     * @param id 抄表数据管理主键
     * @return 抄表数据管理
     */
    @Override
    public ZhwyMeterreadingdata selectZhwyMeterreadingdataById(Long id)
    {

        ZhwyMeterreadingdata zhwyMeterreadingdata = zhwyMeterreadingdataMapper.selectZhwyMeterreadingdataById(id);
        if (zhwyMeterreadingdata.getShopsId()!=null&&!"".equals(zhwyMeterreadingdata.getShopsId())){
            ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(zhwyMeterreadingdata.getShopsId());
            zhwyMeterreadingdata.setCode(zhwyShops.getCode());
        }

        return zhwyMeterreadingdata;
    }

    /**
     * 查询抄表数据管理列表
     * 
     * @param zhwyMeterreadingdata 抄表数据管理
     * @return 抄表数据管理
     */
    @Override
    public List<ZhwyMeterreadingdata> selectZhwyMeterreadingdataList(ZhwyMeterreadingdata zhwyMeterreadingdata)
    {
        System.out.println(zhwyMeterreadingdata);
        ZhwyShops zhwyShops1=new ZhwyShops();
        if (zhwyMeterreadingdata.getCode()!=null&&!"".equals(zhwyMeterreadingdata.getCode())){

            zhwyShops1.setCode(zhwyMeterreadingdata.getCode());
            List<ZhwyShops> shopsList = zhwyShopsMapper.selectZhwyShopsList(zhwyShops1);
            ZhwyShops shop = shopsList.get(0);
            zhwyMeterreadingdata.setShopsId(shop.getId());
        }
        System.out.println(zhwyMeterreadingdata);
//        System.out.println(zhwyShops1);
        List<ZhwyMeterreadingdata> list = zhwyMeterreadingdataMapper.selectZhwyMeterreadingdataList(zhwyMeterreadingdata);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getShopsId()!=null){
                ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(list.get(i).getShopsId());
                list.get(i).setCode(zhwyShops.getCode());
            }

        }
        return list;
    }

    /**
     * 新增抄表数据管理
     * 
     * @param zhwyMeterreadingdata 抄表数据管理
     * @return 结果
     */
    @Override
    public int insertZhwyMeterreadingdata(ZhwyMeterreadingdata zhwyMeterreadingdata)
    {
        ZhwyShops zhwyShops1=new ZhwyShops();
        if (zhwyMeterreadingdata.getCode()!=null&&!"".equals(zhwyMeterreadingdata.getCode())){

            zhwyShops1.setCode(zhwyMeterreadingdata.getCode());
            List<ZhwyShops> shopsList = zhwyShopsMapper.selectZhwyShopsList(zhwyShops1);
            ZhwyShops shop = shopsList.get(0);
            zhwyMeterreadingdata.setShopsId(shop.getId());
        }
//        if (zhwyMeterreadingdata.getLastData()!=null||!"".equals(zhwyMeterreadingdata.getLastData())&&zhwyMeterreadingdata.getThisData()!=null||!"".equals(zhwyMeterreadingdata.getThisData())){
//            Long lastNumber=(zhwyMeterreadingdata.getThisData()-zhwyMeterreadingdata.getLastData())*zhwyMeterreadingdata.getRate()-zhwyMeterreadingdata.getWastage();
//            zhwyMeterreadingdata.setLastNumber(lastNumber);
//        }

        return zhwyMeterreadingdataMapper.insertZhwyMeterreadingdata(zhwyMeterreadingdata);
    }

    /**
     * 修改抄表数据管理
     * 
     * @param zhwyMeterreadingdata 抄表数据管理
     * @return 结果
     */
    @Override
    public int updateZhwyMeterreadingdata(ZhwyMeterreadingdata zhwyMeterreadingdata)
    {
        ZhwyShops zhwyShops1=new ZhwyShops();
        if (zhwyMeterreadingdata.getCode()!=null&&!"".equals(zhwyMeterreadingdata.getCode())){

            zhwyShops1.setCode(zhwyMeterreadingdata.getCode());
            List<ZhwyShops> shopsList = zhwyShopsMapper.selectZhwyShopsList(zhwyShops1);
            ZhwyShops shop = shopsList.get(0);
            zhwyMeterreadingdata.setShopsId(shop.getId());
        }
//        if (zhwyMeterreadingdata.getLastData()!=null||!"".equals(zhwyMeterreadingdata.getLastData())&&zhwyMeterreadingdata.getThisData()!=null||!"".equals(zhwyMeterreadingdata.getThisData())){
//            Long lastNumber=(zhwyMeterreadingdata.getThisData()-zhwyMeterreadingdata.getLastData())*zhwyMeterreadingdata.getRate()-zhwyMeterreadingdata.getWastage();
//            zhwyMeterreadingdata.setLastNumber(lastNumber);
//        }
        return zhwyMeterreadingdataMapper.updateZhwyMeterreadingdata(zhwyMeterreadingdata);
    }

    /**
     * 批量删除抄表数据管理
     * 
     * @param ids 需要删除的抄表数据管理主键
     * @return 结果
     */
    @Override
    public int deleteZhwyMeterreadingdataByIds(Long[] ids)
    {
        return zhwyMeterreadingdataMapper.deleteZhwyMeterreadingdataByIds(ids);
    }

    /**
     * 删除抄表数据管理信息
     * 
     * @param id 抄表数据管理主键
     * @return 结果
     */
    @Override
    public int deleteZhwyMeterreadingdataById(Long id)
    {
        return zhwyMeterreadingdataMapper.deleteZhwyMeterreadingdataById(id);
    }
}
